﻿
Imports System.Data
Imports System.Data.OleDb
Imports System.Drawing

Public Class uploadphoto
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hiddenUserName.Value = Context.User.Identity.Name
    End Sub

    Protected Sub BtnNewPhoto_Click(sender As Object, e As EventArgs) Handles BtnNewPhoto.Click
        Dim PictGUID As New System.Guid
        PictGUID = Guid.NewGuid()
        Dim imgfilename As String = PictGUID.ToString + "Large.jpg"
        Dim smallimgname As String = PictGUID.ToString + "small.jpg"


        Dim imgThumb As Drawing.Image = Nothing


        Dim imgpath As String

        If fileuploadImage.HasFile Then
            imgpath = Request.MapPath("Images")
            fileuploadImage.SaveAs(imgpath & "\" & imgfilename)
            Dim image1 As Drawing.Bitmap = _
          CType(Drawing.Image.FromFile(imgpath & "\" & imgfilename, True), Drawing.Bitmap)
            Dim thheight = image1.Height \ 15
            Dim thwidth = image1.Width \ 15
            If Not image1 Is Nothing Then
                imgThumb = image1.GetThumbnailImage(100, 100, Nothing, New IntPtr())
            End If
            imgThumb.Save(imgpath & "\" & smallimgname)
        End If
        Dim dbdate As DateTime
        Dim dbtime As TimeSpan

        dbdate = Now.Date
        dbtime = Now.TimeOfDay
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("fishconn").ConnectionString)
        Dim SqlString As String = "INSERT INTO tblPhoto ( UserName, Url, SmallUrl, Dateupload, TimeUpload, Caption, LatLoc, LongLoc, Actv )" & _
                                  " Values (@UserName, @Url, @SmallUrl, @Dateupload, @TimeUpload, @Caption, @LatLoc, @LongLoc,'A')"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@UserName", hiddenUserName.Value)
        cmd.Parameters.AddWithValue("@Url", "images\" & imgfilename)
        cmd.Parameters.AddWithValue("@SmallUrl", "images\" & smallimgname)
        cmd.Parameters.AddWithValue("@Dateupload", dbdate)
        cmd.Parameters.AddWithValue("@TimeUpload", dbtime)
        cmd.Parameters.AddWithValue("@Caption", txtCaption.Text)
        cmd.Parameters.AddWithValue("@LatLoc", TxtLat.Text)
        cmd.Parameters.AddWithValue("@LongLoc", TxtLong.Text)
        oleDbConn.Open()
        If cmd.ExecuteNonQuery() = 1 Then
            Response.Redirect("~/Account/myphotos.aspx")
        End If
    End Sub
End Class